home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2006 April / DPPRO0406DVD.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.3.exe / $INSTDIR / Programs / Corridoor.gb < prev    next >
Encoding:
Text File  |  2005-10-05  |  4.0 KB  |  112 lines

  1. ' Quick textured corridor demo
  2. '
  3. ' Notes:
  4. '   * We are using rectangles, so we will be drawing them with glBegin(GL_QUADS)...glEnd()
  5. '   * We will be using multiple textures, so will be storing the texture handles in an array
  6. '   * We will need the Z buffer (also known as "Depth buffer") to ensure the polygons
  7. '     are drawn in the correct order.
  8. '   * We will make the corridor 50 units long, 10 units high and wide
  9.  
  10. ' Data
  11. dim camX#, camY#, camZ#, camAng#    ' Camera position and direction
  12. camX# = 0
  13. camY# = 2
  14. camZ# = 0
  15.  
  16. ' Load textures first
  17. dim tex(4)
  18. tex(1) = LoadTexture ("textures\00001.jpg") ' Left texture
  19. tex(2) = LoadTexture ("textures\00002.jpg") ' Right texture
  20. tex(3) = LoadTexture ("textures\00003.jpg") ' Top texture
  21. tex(4) = LoadTexture ("textures\00004.jpg") ' Bottom texture
  22.  
  23. ' Enable texturing.
  24. ' (Note: We use GL_TEXTURE_2D, because the textures are 2D images.)
  25. glEnable (GL_TEXTURE_2D)
  26.  
  27. ' Setup projection matrix 
  28. ' (What this does takes some explaining, I'll probably put it into a
  29. ' tutorial at some stage..)
  30. glMatrixMode (GL_PROJECTION)
  31. glLoadIdentity ()
  32. gluPerspective (60, (1.0*WindowWidth()) / WindowHeight(), 0.1, 100)
  33. glMatrixMode (GL_MODELVIEW)
  34.  
  35. ' Main loop starts here
  36. while true
  37.     
  38.     ' Draw scene
  39.     
  40.     ' Clear the screen
  41.     glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
  42.         ' Clear the depth buffer (Z buffer)
  43.         ' and the colour buffer (pixels)
  44.  
  45.     ' Position camera
  46.     glLoadIdentity ()
  47.     glRotatef (-camAng#, 0, 1, 0)
  48.     glTranslatef (-camX#, -camY#, -camZ#)
  49.  
  50.     ' Draw the left wall
  51.     glBindTexture (GL_TEXTURE_2D, tex (1))      ' Use texture 1
  52.     glBegin (GL_QUADS)
  53.     
  54.         ' Need texture coordinates and vertex coordinates.
  55.         ' Because the wall 5 times longer than it is high, we want the 
  56.         ' texture to repeat 5 times (otherwise it will appear stretched
  57.         ' out). Therefore we put a 5 into the first texture coordinate.
  58.         glTexCoord2f ( 0, 0): glVertex3f ( -5,  0,  0)
  59.         glTexCoord2f ( 0, 1): glVertex3f ( -5, 10,  0)
  60.         glTexCoord2f ( 5, 1): glVertex3f ( -5, 10,-50)
  61.         glTexCoord2f ( 5, 0): glVertex3f ( -5,  0,-50)
  62.     glEnd ()
  63.     
  64.     ' Draw the top wall
  65.     glBindTexture (GL_TEXTURE_2D, tex (2))      ' Use texture 2
  66.     glBegin (GL_QUADS)
  67.         glTexCoord2f ( 0, 0): glVertex3f (  5,  0,  0)
  68.         glTexCoord2f ( 0, 1): glVertex3f (  5, 10,  0)
  69.         glTexCoord2f ( 5, 1): glVertex3f (  5, 10,-50)
  70.         glTexCoord2f ( 5, 0): glVertex3f (  5,  0,-50)
  71.     glEnd ()
  72.     
  73.     ' Draw the floor
  74.     glBindTexture (GL_TEXTURE_2D, tex (3))      ' Use texture 3
  75.     glBegin (GL_QUADS)
  76.         glTexCoord2f ( 0, 0): glVertex3f (  5,  0,  0)
  77.         glTexCoord2f ( 0, 1): glVertex3f ( -5,  0,  0)
  78.         glTexCoord2f ( 5, 1): glVertex3f ( -5,  0,-50)
  79.         glTexCoord2f ( 5, 0): glVertex3f (  5,  0,-50)
  80.     glEnd ()
  81.  
  82.     ' Draw the ceiling
  83.     glBindTexture (GL_TEXTURE_2D, tex (4))      ' Use texture 4
  84.     glBegin (GL_QUADS)
  85.         glTexCoord2f ( 0, 0): glVertex3f (  5, 10,  0)
  86.         glTexCoord2f ( 0, 1): glVertex3f ( -5, 10,  0)
  87.         glTexCoord2f ( 5, 1): glVertex3f ( -5, 10,-50)
  88.         glTexCoord2f ( 5, 0): glVertex3f (  5, 10,-50)
  89.     glEnd ()
  90.     SwapBuffers ()
  91.  
  92.     ' Move camera
  93.     while SyncTimer (10)
  94.         if ScanKeyDown (VK_LEFT)  then camAng# = camAng# + 1: endif
  95.         if ScanKeyDown (VK_RIGHT) then camAng# = camAng# - 1: endif
  96.         if ScanKeyDown (VK_UP)    then
  97.             camX# = camX# - sind (camAng#) * .2
  98.             camZ# = camZ# - cosd (camAng#) * .2
  99.         endif
  100.         if ScanKeyDown (VK_DOWN)  then
  101.             camX# = camX# + sind (camAng#) * .2
  102.             camZ# = camZ# + cosd (camAng#) * .2
  103.         endif
  104.         
  105.         ' The corridor is rectangle shaped, so we can easily "clamp"
  106.         ' the camera's position inside it
  107.         if camX# < -4 then camX# = -4 endif
  108.         if camX# >  4 then camX# =  4 endif
  109.         if camZ# > -1 then camZ# = -1 endif
  110.         if camZ# <-49 then camZ# =-49 endif
  111.     wend
  112. wend